﻿using Direct.IDAL.Manager.Money;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.Manager;
using System.Collections;
using DirectSell.Model.Money;

namespace DirectSell.BLL.Manager.Money
{
    /// <summary>
    /// 后台电子币审核服务层
    /// </summary>
    public class VirtualMoneyAuditService:ManagerService
    {
        #region 日志记录
        /// <summary>
        /// 日志记录文件夹
        /// </summary>
        protected override string LogFile
        {
            get
            {
                return "电子币审核";
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 电子币操作
        /// </summary>
        protected IVirtualMoneyAuditRepository _repository { get; set; }
        public VirtualMoneyAuditService(IVirtualMoneyAuditRepository repository)
        {
            this._repository = repository;
        }
        #endregion

        #region 操作入口
        /// <summary>
        /// 操作入口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected async override Task<ManagerRespCode> OperationAsync<T>(T param)
        {
            var model = param as VirtualMoneyAudit;

            if (model == null)
            {
                return ManagerRespCode.传入参数不正确;
            }

            await LogAsync(model);

            Hashtable hstable = new Hashtable();

            //0增加 1减少
            var type = await _repository.QueryType(model.Id);

            if (type == 0)
            {
                hstable = await Add(model, hstable);
            }
            else
            {
                hstable = await Reduce(model, hstable);
            }

            if (!_repository.CommitDapperTranLock(hstable))
            {
                return ManagerRespCode.失败;
            }
            return ManagerRespCode.成功;

        }
        #endregion

        #region 增加
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hstable"></param>
        /// <returns></returns>
        private async Task<Hashtable> Add(VirtualMoneyAudit model, Hashtable hstable)
        {
            _repository.UpdateState(model.Id, model.type, ref hstable);

            _repository.AddMoney(model.UserCode, model.Id, ref hstable);

            Consumption money = new Consumption()
            {
                Affair = "后台审核增加电子币",
                CreateDate = DateTime.Now,
                Amout = await _repository.QueryAmount(model.Id),
                LogType = 1,
                Operator = model.UserCode,
                Relevant = model.UserCode
            };

            _repository.InsertConsumption(money, ref hstable);
            return hstable;
        }
        #endregion

        #region 减少
        /// <summary>
        /// 减少
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hstable"></param>
        /// <returns></returns>
        private async Task<Hashtable> Reduce(VirtualMoneyAudit model, Hashtable hstable)
        {
            _repository.UpdateState(model.Id, model.type, ref hstable);

            _repository.ReduceMoney(model.UserCode, model.Id, ref hstable);

            Consumption money = new Consumption()
            {
                Affair = "后台审核减少电子币",
                CreateDate = DateTime.Now,
                Amout = await _repository.QueryAmount(model.Id),
                LogType = 0,
                Operator = model.UserCode,
                Relevant = model.UserCode
            };

            _repository.InsertConsumption(money, ref hstable);
            return hstable;
        }
        #endregion
    }
}
